package com.temi.demo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by coco on 2018/3/18.
 */

public abstract class BaseAdapter<T, BV extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<BV> {
    protected Context mContext;
    protected ItemClickListener mClickListener;
    public List<T> mDataList;

    BaseAdapter(Context context, List<T> dataList) {
        this.mContext = context;
        this.mDataList = dataList;
    }

    public void setData(List<T> dataList) {
        this.mDataList = dataList;
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (position < 0 || position > mDataList.size()) {
            return;
        }

        mDataList.remove(position - 1);
        notifyItemRemoved(position);
    }

    public void Add(T menu, int position) {
        mDataList.add(position, menu);
        notifyItemInserted(position);
    }

    @Override
    public BV onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(getLayout(), parent, false);
        return createVHolder(parent, viewType, view);
    }

    @Override
    public void onBindViewHolder(BV holder, int position) {
        bindViews(holder, position);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public abstract int getLayout();

    public abstract BV createVHolder(ViewGroup parent, int viewType, View view);

    public abstract void bindViews(BV holder, int position);


    public void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }
}
